﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VacationDaysCommons;
using VacationDaysDatabase;

namespace VacationDaysClient
{
    /// <summary>
    /// Interaction logic for RequestEditor.xaml
    /// </summary>
    public partial class RequestEditor : Window
    {
        private VDEmployee currentUser;

        private int vacationDuration;
        private DateTime vacationStartDate;

        public RequestEditor(VDEmployee user)
        {
            InitializeComponent();
            currentUser = user;
        }

        private void startDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            endDatePicker.DisplayDateStart = startDatePicker.SelectedDate;
            vacationStartDate = startDatePicker.SelectedDate.GetValueOrDefault();
        }

        private void endDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (startDatePicker.SelectedDate == null || endDatePicker.SelectedDate == null)
                return;
            var validDays = VDUtils.BusinessDaysUntil(startDatePicker.SelectedDate.GetValueOrDefault(), endDatePicker.SelectedDate.GetValueOrDefault());
            if (validDays > 0)
            {
                durationDaysBox.Text = validDays.ToString();
                vacationDuration = validDays;
                submitButton.IsEnabled = true;

            }
        }

        public void Reset()
        {
            endDatePicker.SelectedDate = null;
            startDatePicker.SelectedDate = null; 
            submitButton.IsEnabled = false;
            durationDaysBox.Text = "";
            durationDaysBox.Clear();
        }

        private void GoSubmitVacationRequest(DateTime VacationStartDate, int VacationDuration, string VacationContext)
        {
            var newVacation = new VDVacationRequest()
            {
                UserID = currentUser.Id,
                StartDate = VacationStartDate,
                Duration = VacationDuration,
                Context = VacationContext,
                RequestState = VDRequestState.Pending
            };


            using (VDRequestServiceClient vr = new VDRequestServiceClient())
            {
                bool success = vr.CreateVacationRequest(newVacation);

                if (success)
                {
                    VDMessageItem messageItem = new VDMessageItem()
                    {
                        UserId = currentUser.PM,
                        SenderId = currentUser.Id,
                        MessageType = VDMessageType.VacationRequested,
                        Timestamp = DateTime.Now,
                        MessageText = newVacation.Context
                    };
                    using (VDMessengerServiceClient vm = new VDMessengerServiceClient())
                    {
                        bool result = vm.SendUserMessage(messageItem);
                    }
                }
                else
                {
                    MessageBox.Show("There was an error creating the user request!");
                }
            }
        }

        private void submitButton_Click(object sender, RoutedEventArgs e)
        {
            GoSubmitVacationRequest(vacationStartDate, vacationDuration, contextBox.Text);
            this.DialogResult = true;
            Close();
        }

        private void resetButton_Click(object sender, RoutedEventArgs e)
        {
            endDatePicker.SelectedDate = null;
            startDatePicker.SelectedDate = null;
            submitButton.IsEnabled = false;
            durationDaysBox.Clear();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            Close();
        }


    }
}
